package com.examine.action;

import com.alibaba.fastjson.JSON;
import com.examine.bean.AccessApplication;
import com.examine.bean.MyApproval;
import com.examine.bean.News;
import com.examine.serviceImpl.AccessApplicationServiceImpl;
import com.examine.serviceImpl.MyApprovalServiceImpl;
import com.examine.serviceImpl.UserServiceImpl;
import com.examine.utils.DateUtil;
import com.examine.utils.StrResponse;
import com.examine.utils.UuidUtil;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.lang3.StringUtils;

import java.util.LinkedHashMap;
import java.util.List;

public class MyApprovalJsonAction extends ActionSupport {
    private LinkedHashMap mymp;
    private MyApprovalServiceImpl myApprovalService;
    private MyApproval myApproval;

    public LinkedHashMap getModelMap() {
        return mymp;
    }
    public void setModelMap(LinkedHashMap mymp) {
        this.mymp = mymp;
    }


    public String addMyApproval(){
        myApproval = JSON.parseObject(StrResponse.getStrResponse(), MyApproval.class);

        if(StringUtils.isEmpty(myApproval.getId())){
            mymp = StrResponse.error("审批ID不能为空");
            return ERROR;
        }

        myApproval.setCreationTime(DateUtil.getCurrentTime());


        myApprovalService = new MyApprovalServiceImpl();
        int count = myApprovalService.insertSelective(myApproval);
        if (count == 0){
            mymp = StrResponse.error("添加审批人信息失败");
            return ERROR;
        }
        mymp = StrResponse.success("添加审批人信息成功");
        return SUCCESS;
    }

    public String queryMyApproval() {
        myApproval = JSON.parseObject(StrResponse.getStrResponse(), MyApproval.class);

        if(StringUtils.isEmpty(myApproval.getId())){
            mymp = StrResponse.error("审批ID不能为空");
            return ERROR;
        }

        myApprovalService = new MyApprovalServiceImpl();
        List<MyApproval> myApprovalList = myApprovalService.shareQueryMyApproval(myApproval);
        if (myApprovalList.size() == 0){
            mymp = StrResponse.error("查询假条失败");
            return ERROR;
        }

        mymp = StrResponse.success("查询假条成功", myApprovalList.get(0));
        return SUCCESS;
    }


    public String updateMyApproval(){
        myApproval = JSON.parseObject(StrResponse.getStrResponse(), MyApproval.class);

        if(StringUtils.isEmpty(myApproval.getId())){
            mymp = StrResponse.error("审批ID不能为空");
            return ERROR;
        }

        myApproval.setUpdateTime(DateUtil.getCurrentTime());


        myApprovalService = new MyApprovalServiceImpl();
        int count = myApprovalService.updateSelective(myApproval);
        if (count == 0){
            mymp = StrResponse.error("修改审批人信息失败");
            return ERROR;
        }
        mymp = StrResponse.success("修改审批人信息成功");
        return SUCCESS;
    }


}
